package com.ssy.lingxi.member.merchant.api.handler.validator;

import com.ssy.lingxi.member.merchant.api.constant.MemberRightTypeEnum;
import com.ssy.lingxi.member.merchant.api.handler.annotation.MemberRightTypeAnno;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 会员权益类型注解校验类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-07-07
 */
public class MemberRightTypeValidator implements ConstraintValidator<MemberRightTypeAnno, Integer> {
    private boolean required = false;
    @Override
    public void initialize(MemberRightTypeAnno constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(!required) {
            return true;
        }

        if(value == null) {
            return false;
        }
        return Arrays.stream(MemberRightTypeEnum.values()).anyMatch(c -> c.getCode().equals(value));
    }
}
